<?php

namespace app\modules\api\controllers;

use Yii;
use yii\web\Controller;
use common\models\Tools;
use common\models\Goods;
use common\models\Cart;
use common\models\GoodsPackage;

/**
 * Default controller for the `api` module
 */
class CartController extends Controller
{
    public $enableCsrfValidation = false;
    /***
     * 检测用户名是否注册过
     * url:/api/default/checkusername-exist
     */
    public function actionAdd(){
        
        if (Yii::$app->user->isGuest) {
            $output['success'] = 0;
            $output['msg'] = "未登录";
            $output['errorcode'] = 1001;
            Tools::outputjson($output);
        }
        $userid = Yii::$app->user->identity->id;
        
        $goods_package_info = Tools::GetSafeParam('goods_package_info', '', 1);
        
        $goods_id =  Tools::GetSafeParam('goods_id', 0, 0);
        $nums =  Tools::GetSafeParam('nums', 0, 0);
        
        if($goods_id == 0){
            $output['success'] = 0;
            $output['msg'] = "错误的商品ID！";
            $output['errorcode'] = 1002;
            Tools::outputjson($output);
        }
        
        $goods = Goods::findOne($goods_id);
        
        if($goods->first_cate==1 && $goods->have_package==0 && $nums<3){
            $output['success'] = 0;
            $output['msg'] = "广告位商品必须购买至少3个月！";
            $output['errorcode'] = 1003;
            Tools::outputjson($output);
        }
        
        if($goods->seller_id == $userid){
            $output['success'] = 0;
            $output['msg'] = "不能购买自己发布的商品！";
            $output['errorcode'] = 1003;
            Tools::outputjson($output);
        }
        
        if($goods->have_package == 1 && $goods_package_info == ""){
            $output['success'] = 0;
            $output['msg'] = "请选择套餐内容！";
            $output['errorcode'] = 1003;
            Tools::outputjson($output);
        }
        
        //不含套餐的商品加入购物车
        if($goods->have_package == 0){
            switch($goods->first_cate){
                case 1://广告位商品，检测是否能够购买
                    if($goods->order_endtime < time() || $goods->last_buyerid == Yii::$app->user->identity->id){
                        //符合购买条件，加入购物车
                        Cart::addNoPackageGoodsToCart($userid,$goods,$nums);
                    }else{
                        $output['success'] = 0;
                        $output['msg'] = "商品暂时不能购买，请等待上次购买到期再来！";
                        $output['errorcode'] = 1004;
                        Tools::outputjson($output);
                    }
                    break;
                case 7://设备商品，检测库存是否符合
                    if($goods->nums >= $nums){
                        Cart::addNoPackageGoodsToCart($userid,$goods,$nums);
                    }else{
                        $output['success'] = 0;
                        $output['msg'] = "库存不足！";
                        $output['errorcode'] = 1005;
                        Tools::outputjson($output);
                    }
                    break;
                default:
                    //do nothing
            }
        }
        //含套餐的商品加入购物车
        if($goods->have_package == 1){
            $price = 0;
            $price_total = 0;
            $install_price = 0;
            $packages = explode(';', $goods_package_info);
            foreach($packages as $package){
                $package_one = explode(',', $package);
                $package_id = $package_one[0];
                $package_buy_num = $package_one[1];
                $package_model = GoodsPackage::findOne($package_id);
                $price += $package_buy_num*$package_model->price;
                $install_price += $package_buy_num*$package_model->install_price;
                $price_total += ($package_model->price+$package_model->install_price)*$package_buy_num;
                
                switch($goods->first_cate){
                    case 1://广告位商品，检测是否能够购买
                        if($package_model->buy_able_time < time() || $package_model->last_buyerid == $userid){
                            //符合条件,计算价格，和安装费用

                        }else{
                            $output['success'] = 0;
                            $output['msg'] = "套餐【$package_model->name】暂时不能购买，请等待上次购买到期再来！";
                            $output['errorcode'] = 1006;
                            Tools::outputjson($output);
                        }
                        //添加含套餐的商品到购物车
                        Cart::addPackageGoodsToCart($userid,$goods,$price,$install_price,$price_total,$goods_package_info);
                        break;
                    case 7://设备商品，检测套餐 库存是否符合
                        if($package_model->nums >= $package_buy_num){
                            //符合条件,计算价格，和安装费用
                        }else{
                            $output['success'] = 0;
                            $output['msg'] = "套餐【$package_model->name】库存不足！";
                            $output['errorcode'] = 1007;
                            Tools::outputjson($output);
                        }
                        //添加含套餐的商品到购物车
                        Cart::addPackageGoodsToCart($userid,$goods,$price,$install_price,$price_total,$goods_package_info);
                        break;
                    default:
                        //do nothing
                }
            }
            
        }
    }
}
